<?php
function scheduler($teams){ 
// Check for even number or add a bye 
    if (count($teams)%2 != 0){ 
        array_push($teams,"bye"); 
    } 
// Splitting the teams array into two arrays 
    $away = array_splice($teams,(count($teams)/2)); 
    $home = $teams; 
// The actual scheduling based on round robin 
    for ($i=0; $i < count($home)+count($away)-1; $i++){ 
        for ($j=0; $j<count($home); $j++){ 
            $round[$i][$j]["Home"]=$home[$j]; 
            $round[$i][$j]["Away"]=$away[$j]; 
        } 
// Check if total numbers of teams is > 2 otherwise shifting the arrays is not neccesary 
        if(count($home)+count($away)-1 > 2){ 
            array_unshift($away,array_shift(array_splice($home,1,1))); 
            array_push($home,array_pop($away)); 
        } 
    } 
    return $round; 
}


   function generate_numbers( $min, $max, $anz )
{
    $array = range( $min, $max );
    srand( (double)microtime() * 1000000 );
    for ( $x=0; $x<$anz; $x++ ) {
        $i = rand( 1, count( $array) ) - 1;
        $erg[] = $array[$i];
        array_splice( $array, $i, 1 );
    }
    return $erg;
}
 

 
 $teams5 = array(
  0 => "LUCK",
  1 => "TsZ",
  2 => "MoR",
  3 => "VR",
  4 => "DNS",
  5 => "SoD",
  6 => "EoN",
  7 => "Freilos",
  8 => "BRC",
  9 => "Dark",
  );
 
 
 $i = 0;
 while($i < count($teams5)){
  echo $teams5[$i]. '</br>';
  $i++;
 }
 ?>
<form action="rundenziehung.php">
  <p>
    <input type="submit" name="ziehenbtn" value="Ziehen">
  </p>
</form>
<?php
  $ziehen = htmlspecialchars($_GET['ziehenbtn']);

if($ziehen == "Ziehen"){  

    $test = array();
    $zufalls_array = generate_numbers( 0, 9, 10);
    $i = 0;
    echo '<strong>Teams f&uuml;r die Liga</strong><br>';
    while($i < 10){
      array_push($test, $teams5[$zufalls_array[$i]]);
      $i++;
    }  

$schedule = scheduler($test);

  foreach($schedule AS $round => $games){ 
      echo "Runde: ".($round+1)."<BR>"; 
      foreach($games AS $play){ 
          echo $play["Home"]." vs ".$play["Away"]."<BR>"; 
      } 
      echo "<BR>"; 
  } 
  
}else{
  $schedule = scheduler($teams5);

  foreach($schedule AS $round => $games){ 
      echo "Runde: ".($round+1)."<BR>"; 
      foreach($games AS $play){ 
          echo $play["Home"]." vs ".$play["Away"]."<BR>"; 
      } 
      echo "<BR>"; 
  } 
}       
                          
?>